//
//  Date+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import Foundation

public extension Date {
    
    // MARK: - 获取当前时区Date
    static func getCurrentDate() -> Date {
        let nowDate = Date()
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: nowDate)
        let localeDate = nowDate.addingTimeInterval(TimeInterval(interval))
        return localeDate
    }
    
    //UTC 时区
    static func stringFromDate(dateString:String, formatter:String = "HH:mm:ss") -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        return dateFormatter.date(from: dateString)
    }
    
    static func dateFromString(date:Date, formatter:String = "HH:mm:ss") -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        return dateFormatter.string(from: date)
    }
    
    //默认时区
    static func stringFromDateZone(dateString:String, formatter:String = "HH:mm:ss") -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        return dateFormatter.date(from: dateString)
    }
    static func dateFromStringZone(date:Date, formatter:String = "HH:mm:ss") -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        return dateFormatter.string(from: date)
    }
    
    
    
    /// 年
    var year: Int {
        return NSCalendar.current.component(.year, from: self)
    }
    /// 月
    var month: Int {
        return NSCalendar.current.component(.month, from: self)
    }
    /// 日
    var day: Int {
        return NSCalendar.current.component(.day, from: self)
    }
    /// 周几
    var weekday: String {
        let weekdays = [NSNull.init(),"星期天","星期一","星期二","星期三","星期四","星期五","星期六"] as [Any]
        var calendar = Calendar(identifier:.gregorian)
        
        let timeZone = NSTimeZone.init(name: "Asia/Shanghai")
        
        calendar.timeZone = timeZone! as TimeZone
        
        let theComponents = calendar.dateComponents([.year,.month, .day,.weekday, .hour,.minute,.second], from: self)
        
        return weekdays[theComponents.weekday!] as! String
    }
    
    /// 是否在将来
    var isFuture: Bool {
        return self > Date()
    }
    
    /// 是否在过去
    var isPast: Bool {
        return self < Date()
    }
    /// 是否是今天
    var isToday: Bool {
        let format = DateFormatter()
        format.dateFormat = "yyyy-MM-dd"
        return format.string(from: self) == format.string(from: Date())
    }
    
    /// 是否是昨天
    var isTomorrow: Bool {
        let format = DateFormatter()
        format.dateFormat = "yyyy-MM-dd"
        let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())
        return format.string(from: self) == format.string(from: tomorrow!)
    }
    
    /// 获取当前 秒级 时间戳 - 10位
    var timeStamp : TimeInterval {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        return timeInterval
    }
    
    /// 获取当前 毫秒级 时间戳 - 13位
    var milliStamp : Int64 {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = Int64(round(timeInterval*1000))
        return millisecond
    }
    
    // MARK: - 时间戳转换成Date
    static func timestampWithDate(second: Double) -> Date {
        let date = Date.init(timeIntervalSince1970: second)
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: date)
        let localeDate = date.addingTimeInterval(TimeInterval(interval))
        return localeDate
    }
}
